package it.unibo.iss.domotica.system;
import org.eclipse.core.runtime.adaptor.EclipseStarter;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;

public class Starter {
    private static final String path = "file:" +
			(( System.getProperty("libPath") != null ) ? 
			System.getProperty("libPath") : "lib/bundles/" );
	private BundleContext context;

	public void doJob() throws Exception {
		loadMySystem();
		loadBundle(path + "it.unibo.tuprolog_1.0.1.jar");
		loadBundle(path + "it.unibo.is.interfaces_1.1.3.jar");		//requires  tuprolog
		loadBundle(path + "it.unibo.platform.lindalike_1.1.4.jar");	//requires  interfaces tuprolog
// 		loadBundle(path + "it.unibo.platform.tcp_1.0.2.jar");		//requires interfaces lindalike
 		loadBundle(path + "it.unibo.platform.expert_1.1.4.jar");	//requires  interfaces tcp, http, ...
  		loadBundle(path + "it.unibo.platform.medcl_1.3.4.jar");		//requires interfaces lindalike platform.expert
//		loadBundle(path + "it.unibo.platform.udp_1.0.0.jar");		//requires interfaces lindalike
//		loadBundle(path + "it.unibo.platform.tcp_1.0.0.jar");		//requires interfaces lindalike
//		loadBundle(path + "org.apache.commons.logging_1.0.4.v20080605-1930.jar");
//		loadBundle(path + "org.apache.commons.codec_1.3.0.v20080530-1600.jar");
//		loadBundle(path + "org.apache.commons.httpclient_3.1.0.v20080605-1935.jar"); //requires commons.logging codec interfaces lindalike
//		loadBundle(path + "it.unibo.platform.http_1.0.0.jar");
		loadBundle(path + "it.unibo.domoticaContact_1.0.0.jar");		//requires  medcl
	}

/*
Equinox (the Eclipse OSGi runtime) can be launched via a Java process 
if you have an existing Java application that needs integration. 
You can of course run it by putting startup.jar on the classpath, 
and running org.eclipse.core.launcher.Main 's main or run methods. 

Alternatively, you can use org.eclipse.core.runtime.adaptor.EclipseStarter , 
which is in the org.eclipse.osgi Jar. It has main and run methods as well, 
so either of these can be used. The parsing of the arguments is the same 
as launching from the command line . 

    Bundle bundle = context.installBundle(
      "http://www.eclipsezone.com/files/jsig/bundles/HelloWorld.jar");

Of course, this a trivial example, but the Java application could be a servlet, 
GUI client (including Swing) or middleware in a message oriented application; 
the principle is the same. The only tricky part will be ensuring that any place 
this is used manages a single instance of the OSGi engine; Equinox uses a number 
of paths (e.g. the osgi.configuration.area and osgi.instance.area ) that need 
to be customised on a per-OSGi instance. 

*/
	protected void loadMySystem() throws Exception {
		String[] equinoxArgs = new String[]{"-console"};//(1)
		context = EclipseStarter.startup(equinoxArgs, null);
		System.out.println(""); 
		System.out.println("*** Starter"); 
//		System.out.println("*** Starter osgi.bundlestore=" + 
//							context.getProperty("osgi.bundlestore") );
	}

	protected void loadBundle(String locationBundle)  {
		Bundle bundle;
		try {
			bundle = context.installBundle(locationBundle);
			bundle.start();
		} catch (BundleException e) {
			System.out.println("ERROR " + e);
		}
	}

	public static void main(String args[]) throws Exception {
		Starter starter = new Starter();
		starter.doJob();
	}
}
